package com.rop.znyx.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rop.znyx.common.result.Result;
import com.rop.znyx.model.acl.Role;
import com.rop.znyx.acl.service.RoleService;
import com.rop.znyx.vo.acl.RoleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Created with IntelliJ IDEA
 * User: Rop
 * Date: 2023/6/13
 * Time: 15:23
 * Description:
 */

@Api(tags = "角色管理")
@RestController
@RequestMapping("admin/acl/role")
@RequiredArgsConstructor

public class RoleController {

    private final RoleService roleService;

    /**
     * 获取角色分页列表
     *
     * @param page        页数
     * @param limit       条数
     * @param roleQueryVo 查询的实体
     * @return 查询的列表
     */
    @ApiOperation(value = "获取角色分页列表")
    @GetMapping("{page}/{limit}")
    public Result<IPage<Role>> getList(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable("page") Long page,
                                       @ApiParam(name = "limit", value = "条数", required = true) @PathVariable("limit") Long limit,
                                       @ApiParam(name = "roleQueryVo", value = "查询对象", required = false) RoleQueryVo roleQueryVo) {
        Page<Role> pageParam = new Page<>(page, limit);
        IPage<Role> pageModel = roleService.selectPage(pageParam, roleQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 根据id获取角色
     *
     * @param id 角色id
     * @return 角色详情
     */
    @ApiOperation("根据id获取角色")
    @GetMapping("getById/{id}")
    public Result<Role> getById(@PathVariable("id") Long id) {
        if (id != null) {
            Role role = roleService.getById(id);
            return Result.ok(role);
        }
        return Result.fail();
    }

    /**
     * 新增角色
     *
     * @param role 角色实体
     * @return 新增结果
     */
    @ApiOperation("新增角色")
    @PostMapping("save")
    public Result<Boolean> save(@RequestBody Role role) {
        boolean success = roleService.save(role);
        return success ? Result.ok() : Result.fail();
    }

    /**
     * 修改角色
     *
     * @param role 角色实体
     * @return 新增结果
     */
    @ApiOperation("修改角色")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody Role role) {
        boolean success = roleService.updateById(role);
        return success ? Result.ok() : Result.fail();
    }

    /**
     * 删除角色
     *
     * @param id 角色id
     * @return 删除结果
     */
    @ApiOperation("删除角色")
    @DeleteMapping("deleteById/{id}")
    public Result<Boolean> deleteById(@PathVariable("id") Long id) {
        boolean success = roleService.removeById(id);
        return success ? Result.ok() : Result.fail();
    }

    /**
     * 批量删除角色
     *
     * @param ids 角色ids
     * @return 删除结果
     */
    @ApiOperation("批量删除角色")
    @DeleteMapping("deleteByIds")
    public Result<Boolean> deleteByIds(@RequestBody List<Long> ids) {
        boolean success = roleService.removeByIds(ids);
        return success ? Result.ok() : Result.fail();
    }
}
